from requests import Session
from bs4 import BeautifulSoup
import translator
import utils

session = Session()
session.trust_env = False
session.adapters.DEFAULT_RETRIES=10
session.headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}

def scrap_all_server_rate(translate=True):
    resp=session.get("https://ark.fandom.com/wiki/Template:RailModule")
    soup = BeautifulSoup(resp.content,"lxml")
    wrapper=soup.find("div",class_="wds-tabs__wrapper")
    server_names=[a.get_text(strip=True) for a in wrapper.select("a")]
    server_panes = soup.select(".wds-tab__content>ul")
    if len(server_names)!=len(server_panes):
        raise ValueError("服务器数量与配置的数量不一致")
    server_rates=[]
    for i in range(len(server_names)):
        server_rate={}
        server_rate["serverName"]=server_names[i]
        server_rate["rates"]=[]
        for li in server_panes[i].select("li"):
            rate={}
            rate["rate"]=li.next_element.string
            rate["data"]=[a.get_text() for a in li.select("a")]
            server_rate["rates"].append(rate)
        server_rates.append(server_rate)
    # 翻译数据
    if translate:
        translator.translate_server_rates(server_rates,update_translations=True)
    # 保存数据
    utils.save_data(server_rates, "data/server-rates.json")
